package com.wss.lsl.alading.controller.index;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.wss.lsl.alading.service.ILoginService;

@Controller
public class LoginController {

	Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private ILoginService loginService;

	@RequestMapping("/")
	public String index() {
		logger.trace("entrace index!");
		
		return "login";
	}

	@RequestMapping("/login")
	public String login(@RequestParam("username") String username,
			@RequestParam("password") String password, Model model,
			HttpSession session) {
		if (StringUtils.isNotEmpty(username)
				&& StringUtils.isNotEmpty(password)
				&& loginService.checkUsernamePassword(username, password)) {

			session.setAttribute("username", username);
			return "redirect:main";
		} else {
			model.addAttribute("error", true);
			return "login";
		}
	}
	
	// 主窗口
	@RequestMapping("/main")
	public String mainWindow() {
		return "main";
	}

	@RequestMapping("/logout")
	public String login(HttpSession session) {
		try {
			session.invalidate();
		} catch (Exception e) {
		}
		return "login";
	}
}
